#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include <Timer2ServoPwm.h>

SoftwareSerial mySerial(10, 11);

// SoftwareSerial mySerial1(12,13);
volatile char duoji;
Adafruit_NeoPixel rgb_display_11 = Adafruit_NeoPixel(60,11,NEO_GRB + NEO_KHZ800);

DFRobotDFPlayerMini myPlayer;
volatile int chao1;
volatile int chao2;
Timer2Servo servo_5;
int flag=0;
int flag1=0;

float checkdistance_A2_A3() {
  digitalWrite(A2, LOW);
  delayMicroseconds(2);
  digitalWrite(A2, HIGH);
  delayMicroseconds(10);
  digitalWrite(A2, LOW);
  float distance = pulseIn(A3, HIGH) / 58.00;
  delay(10);
  return distance;
}

float checkdistance_A4_A5() {
  digitalWrite(A4, LOW);
  delayMicroseconds(2);
  digitalWrite(A4, HIGH);
  delayMicroseconds(10);
  digitalWrite(A4, LOW);
  float distance = pulseIn(A5, HIGH) / 58.00;
  delay(10);
  return distance;
}

void setup() {
  // put your setup code here, to run once:
  duoji = 0;
  servo_5.attach(5);
  //超声波测距
  pinMode(A2, OUTPUT);
  pinMode(A3, INPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, INPUT);

  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);

  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);

  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);

  Serial.begin(115200);
  mySerial.begin(9600);
  
  // Serial.begin(9600);
  // mySerial1.begin(9600);
}

void loop() {
  chao1 = checkdistance_A2_A3();
  chao2 = checkdistance_A4_A5();
  int angle = 0;   // 初始化角度0上升
  
  // 检查蓝牙模块是否有数据
  if (mySerial.available() > 0) {
    char command = mySerial.read();
    Serial.print("Received command: ");
    Serial.println(command);
    if(command == 'a') {
      
      servo_5.write(90);//下降
      digitalWrite(3, HIGH);
      delay(100);
      digitalWrite(3, LOW);
      // digitalWrite(3, LOW);
      /*int pin3State = digitalRead(3); // 读取引脚3的当前状态
      digitalWrite(3, !pin3State); // 使用逻辑非操作符来翻转状态
      delay(200); // 保持翻转状态一段时间*/
      
    }
    else if(command == 'b')//上升
    { 
     
      servo_5.write(10);
       digitalWrite(4, HIGH);
       delay(100);
      digitalWrite(4, LOW);
    }
    else if(command == 'c')//识别失败
    { 
     
     
       digitalWrite(2, HIGH);
       delay(100);
      digitalWrite(2, LOW);
    }
  }
  if (Serial.available() > 0) {
    int incomingByte = Serial.read();  // 读取串口数据
    if (incomingByte >= '0' && incomingByte <= '9') {
      int num = incomingByte - '0';  // 将ASCII码转换为数字
      Serial.print("I received number: ");
      Serial.println(num);  // 打印接收到的数字
  if(num==1&&flag==0)
  {
    servo_5.write(90);//下降
    digitalWrite(3, HIGH);
    delay(50);
    digitalWrite(3, LOW);
    delay(10000);
    flag=1;
  }
  else if(chao2 > 180 && chao1 > 180&&flag==1)//上升
  {
      servo_5.write(10);
      digitalWrite(4, HIGH);
      delay(100);
      digitalWrite(4, LOW);
      flag=2;
  }


  // else if(num==0&&flag==1)
  // {
  //   servo_5.write(0);
  //   digitalWrite(4, HIGH);
  //   delay(100);
  //   digitalWrite(4, LOW);
  // }
    }
    
  }
  
  // if((chao2 < 180 || chao1 < 180)&&num==1)
  // {
  //     servo_5.write(90);//下降
  //     digitalWrite(3, HIGH);
  //     delay(200);
  //     digitalWrite(3, LOW);
  // }
  //  else if(chao2 > 180 && chao1 > 180&&num==0)//上升
  // {
  //     servo_5.write(0);
  //     digitalWrite(4, HIGH);
  //     delay(100);
  //     digitalWrite(4, LOW);
  // }
  
// ||chao2 < 180 || chao1 < 180
// ||chao2 > 180 && chao1 > 180

  //打印测量到的距离
  // Serial.print("测量距离: ");
  // Serial.print(chao1);
  // Serial.println(" cm");

  // delay(500); // 等待500毫秒再次测量




}